<template>
  <!-- 新增价格分类 -->
  <div>
    <el-dialog
      v-el-drag-dialog
      :title="type==='add'?'新增价格分类':'修改价格分类'"
      :visible.sync="visible"
      width="500px"
      @close="reset"
    >
      <el-form ref="form" class="form" :model="query" label-position="right" label-width="80px">
        <el-form-item label="组织机构:" prop="csmRgPhone">
          <el-tree-select
            v-model="query.orgSNNo"
            :disabled="type==='edit'"
            :clearable="false"
            class="tree-select"
            :popover-width="360"
            :select-params="{
              clearable: true,
              placeholder: '请选择',
              loading: $loadingPlugin.getTreeData
            }"
            :tree-params=" {
              'expand-on-click-node': false,
              'highlight-current': true,
              'filterable': true,
              props:{
                value:'orgSNNo',
                label: 'orgName',
                disabled: 'disabled',
              },
              data: treeData
            }"
            @node-click="selectTreeChange"
          />
        </el-form-item>
        <el-form-item label="名称:" prop="cardNo">
          <el-input v-model.trim="query.name" placeholder="请输入名称" maxlength="30" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="save">保 存</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import getTreeData from '@/utils/jsonTree'
  import { mapGetters } from 'vuex'
  import { defaultPaymentMethodMap } from '@/utils/map'
  import { queryPriceTypeAdd, queryPriceTypeUpdate } from '@/api'

  export default {
    name: 'addPriceCategory',
    data() {
      return {
        visible: false,
        type: 'add',
        defaultPaymentMethodMap,
        treeData: [],
        temp: {},
        query: { name: null },
        orgObj: {}
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    async created() {
      await this.getTreeData()
    },
    methods: {
      async save() {
        if (!this.orgObj.orgId) {
          this.$message.warning('请选择机构')
          return
        }
        if (!this.query.name) {
          this.$message.warning('请输入分类名称')
          return
        }
        const params = {
          orgId: this.orgObj.orgId,
          orgName: this.orgObj.orgName,
          orgSNNo: this.orgObj.orgSNNo,
          priceCgId: this.orgObj.priceCgId,
          priceCgName: this.query.name
        }
        if (this.type === 'add') {
          var res = await queryPriceTypeAdd(params)
          if (res.success) {
            this.$message.success('添加成功')
            this.reset()
            this.$emit('refresh')
          }
        } else if (this.type === 'edit') {
          res = await queryPriceTypeUpdate(params)
          if (res.success) {
            this.$message.success('修改成功')
            this.reset()
            this.$emit('refresh')
          }
        }
      },
      // 获取所属机构数据
      async getTreeData() {
        // const res = await organizationQueryList({})
        const data = this.orgList.map(item => ({
          label: item.name,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      // 选择所属机构
      selectTreeChange(value) {
        this.orgObj = value
        // this.orgSNNo = value.orgSNNo
      },
      reset() {
        this.orgObj = {}
        this.query = { name: null }
        this.visible = false
      },
      // 打开
      open(query) {
        return new Promise((resolve) => {
          this.resolve = resolve
          this.visible = true
          this.type = 'add'
          if (query) {
            this.type = 'edit'
            // console.log(query, '111')
            this.orgObj = { ...query }
            this.query.name = query.priceCgName
            this.query.orgSNNo = query.orgSNNo
            // this.query = Object.assign(this.query, query)
          }
        })
      }
    }
  }
</script>

<style lang='scss' scoped>

</style>
